import os
from pathlib import Path
from importlib import import_module
from rest_framework import routers
from django.urls import path, include
from plugin.controllers.plugin import PluginViewSet

router = routers.DefaultRouter(trailing_slash=False)
router.register(r'plugins', PluginViewSet)
urlpatterns = [
    path(r'', include(router.urls)),
]

cur_dir = Path(__file__).resolve().parent
plugin_dir = os.path.join(cur_dir, 'plugins')
plugin_names = [item for item in os.listdir(plugin_dir) if os.path.isdir(os.path.join(plugin_dir, item))]
for plugin_name in plugin_names:
    urls_module = import_module(f"plugin.plugins.{plugin_name}.urls")
    plugin_urlpatterns = getattr(urls_module, "urlpatterns", [])
    urlpatterns += plugin_urlpatterns
